import HttpClint from './request.js'

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return `${[year, month, day].map(formatNumber).join('-')}`
}

const currentTime = () => {
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const currentMonth = () => {
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  return `${[year, month].map(formatNumber).join('-')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const getEnvironment = data => {
  try {
    const res = wx.getSystemInfoSync()
    console.log(res)
    // console.log(res.environment)
    wx.setStorageSync('environment', !!res.environment)
  } catch (e) {
    // Do something when catch error
  }
}

// const getDict = async function (code) {
//   let dictList = wx.getStorageSync(code)
//   let data
//   if (!dictList) {
//     data = await HttpClint.request({
//       url: "/system/dict/data/type/week_type",
//       method: 'GET'
//     })
//   }
//   console.log('data', data)
//   return dictList
// }

const getUserInfo = async function () {
  let userInfo = wx.getStorageSync('userInfo')
  if (!userInfo) {
    userInfo = await HttpClint.request({
      url: "/user/getInfo",
      method: 'GET'
    })
  }
  console.log(userInfo)
  return userInfo
}

module.exports = {
  formatTime,
  getEnvironment,
  getUserInfo,
  currentTime,
  currentMonth
}